/**
 * Created by JetBrains PhpStorm.
 * User: krisbuist
 * Date: 1/20/12
 * Time: 11:42 AM
 */

$('#tag-input').keyup(function () {
    addTag(this, false);
});

$('#tag-input').change(function () {
    addTag(this, true);
});

function addTag(element, force) {
    var curValue = element.value;
    if (curValue.charAt(curValue.length - 1) == ',' || force) {
        //var tagName = jQuery.trim(curValue.substring(0, curValue.length - (force ? 0 :1)));
        var tagName = jQuery.trim(curValue.replace(",", ""));
        if (tagName != "") {
            $('#tag-container').append('<span class="label tag-label" onclick="$(this).remove()">' + tagName + '<input type="hidden" name="tags[]" value="' + tagName + '" /></span>');
            element.value = '';
        }
    }
}

$("#moreImages").click(function(){
    var i = 1;
    var j = $("#userfile_" +i).length;
    while(j > 0)
    {
        i++;
        j = $("#userfile_" +i).length;
    }
    $('#file-upload-container').append("<input name=\"userfile_"+i+"\" id=\"userfile_"+i+"\" size=\"40\" type=\"file\" /><input type=\"text\" name=\"description_uf_"+i+"\" size=\"40\" placeholder=\"Beschrijving\" \><hr \>");
    if(i==1)
    {
        $('#moreImages').attr('value', 'Voeg nog een afbeeding toe');
    }
});

$('#titleSearchPagination > ul > li > span').click(function()
{
    var page = $(this).attr('page');
    $('#titleSearchResultsBody').load('/search/titlePaginated/' + searchString + '/' + page);
    $('#titleSearchPagination > ul > li').removeClass('active');
    $(this).parent().addClass('active');
});

$('#tagSearchPagination > ul > li > span').click(function()
{
    var page = $(this).attr('page');
    $('#tagSearchResultsBody').load('/search/tagPaginated/' + searchString + '/' + page);
    $('#tagSearchPagination > ul > li').removeClass('active');
    $(this).parent().addClass('active');
});

$('#authorSearchPagination > ul > li > span').click(function()
{
    var page = $(this).attr('page');
    $('#authorSearchResultsBody').load('/search/authorPaginated/' + searchString + '/' + page);
    $('#authorSearchPagination > ul > li').removeClass('active');
    $(this).parent().addClass('active');
});

$('#mediaSearchPagination > ul > li > span').click(function()
{
    var page = $(this).attr('page');
    $('#mediaSearchResultsBody').load('/search/mediaPaginated/' + searchString + '/' + page);
    $('#mediaSearchPagination > ul > li').removeClass('active');
    $(this).parent().addClass('active');
});